# -*- coding: utf-8 -*-
#   Copyright 2009, 2010 Thomas Olsen <tanghus@gmail.com>
#
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License as
#   published by the Free Software Foundation; either version 2 or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#
#   GNU General Public License for more details
#
#
#   You should have received a copy of the GNU Library General Public
#   License along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#

from PyQt4.QtCore import QObject, QVariant #, SIGNAL
#from PyQt4.QtGui import *
from PyKDE4.kdecore import i18n, KComponentData, KGlobal
#from PyKDE4.kdeui import KIcon
from PyKDE4.kio import KIO
from PyKDE4.plasma import Plasma
#from PyKDE4.solid import *
from PyKDE4.plasmascript import Applet
from distutils import version
from utils import fixType
import commands
from mylogging import debug

class BasePlasmoid(Applet):

  def __init__(self,parent,args=None):
    Applet.__init__(self, parent)
    
  def init(self):
    print "init() in BasePlasmoid"
    self.setBusy(True)
    self.kdehome = unicode(KGlobal.dirs().localkdedir())
    self.metadata = self.package().metadata()
    self.show_metadata()
    #self.icon = KIcon(u"%scontents/icons/%s.png" % (self.package().path(), self.metadata.pluginName())) #.pixmap(QSize(22,22))
    self.cfg = self.config(self.metadata.pluginName())
    
    # Check version
    old = version.LooseVersion(fixType(self.cfg.readEntry("version", "0.0.0")))
    new = version.LooseVersion(str(self.metadata.version()))
    if old < new:
      print u"Updating from version '%s' to '%s'" % (str(old), str(new))
      self.update_version()

  def install_icon(self):
    out = commands.getstatusoutput("xdg-icon-resource install --size 64 --context apps " + unicode(self.package().path()) + "contents/icons/%s.png %s" % (self.metadata.pluginName(), self.metadata.pluginName()))        
    if out[0] == 0:
      print u"%s icon installed" % self.metadata.pluginName()
    else:
      print u"Error installing %s icon:" % self.metadata.pluginName()

  def update_version(self):
    self.install_icon()
    self.cfg.writeEntry("version", self.metadata.version())

  def show_metadata(self):
    print u"Name: %s" % self.metadata.name()
    print u"Description: %s" % self.metadata.description()
    print u"Version: %s" % self.metadata.version()
    print u"Category: %s" % self.metadata.category()
    print u"Author: %s" % self.metadata.author()
    print u"Plugin name: %s" % self.metadata.pluginName()

  